package com.lancoo.answer.view.fragment.correctmistake

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Rect
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.SparseArray
import android.view.KeyEvent
import android.view.View
import android.view.ViewTreeObserver
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvFmtGctPracticeBinding
import com.lancoo.answer.helper.NoConfirmDialogHelper
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.ModelAnswerArea
import com.lancoo.answer.model.bean.ModelAnswerInfo
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.model.bean.TextInfo
import com.lancoo.answer.util.*
import com.lancoo.answer.view.adapter.CorrectMistakesTextAdapter
import com.lancoo.answer.view.base.BaseBindingFragment
import com.lancoo.answer.widget.DrawView
import com.lancoo.answer.widget.NewEditInputView
import com.lancoo.answer.widget.PopupList
import com.rxjava.rxlife.RxLife
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern

/**
 * Created by wealive on 2021/9/15.
 *
 *
 */
class PracticeCorrectMistakesFragment : BaseBindingFragment<EvFmtGctPracticeBinding>(),
    PopupList.PopupListListener {
    private val popupMenuItemList = arrayListOf<String>()
    private val tvMap = SparseArray<View>()

    //    private val itemAnswer = hashMapOf<Int,String>()
    private val askList = ArrayList<TextInfo>()
    private val textInfoList = mutableListOf<TextInfo>()
    private lateinit var adapter: CorrectMistakesTextAdapter
    private var answerInfoList = mutableListOf<ModelAnswerInfo>()
    private lateinit var inputPopupWindow: NewEditInputView

    private var currentOperateIndex = -1 //当前操作的单词下标
    private var remaindStyle = false
    private var currentRalY = 0
    private var lastRalY = 0
    private var GenreType = 0
    private var GenreID = 0
    private var answerArea = mutableListOf<ModelAnswerArea>()
    private lateinit var inputManager: InputManager
    private lateinit var ques: Ques
    private var typeIndex = 0
    private var quesIndex = 0
    private var defaultLoadIndex = 0
    private var screenHeight = 0
    private var currentIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initAnswerData()
    }

    companion object {
        fun loadFragment(typeIndex: Int, quesIndex: Int, defaultLoadIndex: Int): Fragment {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt("loadQuesIndex", defaultLoadIndex)
            val practiceCorrectMistakesFragment = PracticeCorrectMistakesFragment()
            practiceCorrectMistakesFragment.arguments = mBundle
            return practiceCorrectMistakesFragment
        }
    }

    /*初始化题干及参考答案*/
    private fun initAnswerData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        defaultLoadIndex = requireArguments().getInt("loadQuesIndex")
        currentIndex = defaultLoadIndex
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        screenHeight = requireActivity().window.decorView.rootView.height//获取屏幕高度
        GenreType = ques.GenreType!!.toInt()
        GenreID = ques.GenreID!!.toInt()
        val answerAreaList =
            ques.ModelAnswerAreaList
        for (i in answerAreaList!!.indices) {
            val area = answerAreaList[i]
            answerArea.add(area)
        }
        textInfoList.addAll(
            ques.ModelTextInfoList!!
        )
        answerInfoList.addAll(
            ques.ModelAnswerInfoList!!
        )
        val subChildItemBeanXList =
            ques.ChildList!![0].ItemList
        for (i in answerInfoList.indices) {
            var digitalAnswer = subChildItemBeanXList!![i].StuAnswer
            if (digitalAnswer == null) {
                digitalAnswer = ""
            }
            val stuAnswerInfo = digitalAnswer.split("@|")[0].split("*|")
            var answer: String
            if (stuAnswerInfo.size >= 2) {
                val position = stuAnswerInfo[0]
                if (position != "-1") {
                    val type = stuAnswerInfo[1].toInt()
                    answer = stuAnswerInfo[2]
                    if (position.contains("-")) {
                        textInfoList[getTextPosition(position.split("-")[0].toInt())].digitalAnswer =
                            digitalAnswer
                        textInfoList[getTextPosition(position.split("-")[0].toInt())].stuType = type
                        textInfoList[getTextPosition(position.split("-")[0].toInt())].stuAnswer =
                            answer
                    } else {
                        textInfoList[getTextPosition(position.toInt())].digitalAnswer =
                            digitalAnswer
                        textInfoList[getTextPosition(position.toInt())].stuType = type
                        textInfoList[getTextPosition(position.toInt())].stuAnswer = answer
                    }
                }
            }
        }


        /*插入参考答案及初始化学生作答*/
        for (i in subChildItemBeanXList!!.indices) {
            val itemBeanX = subChildItemBeanXList[i]
            if (!itemBeanX.ItemAnswer!!.contains("@")) {
                itemBeanX.ItemAnswer = answerInfoList[i].Answer + "@|" + itemBeanX.ItemAnswer
            }
        }
//        adjustFontSize()
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        inputManager = InputManager(context)
        binding.apply {
            if (!ques.QuesIndexText.isNullOrEmpty()) {
                tvLeader.text = HtmlUtils.setText(ques.QuesIndexText!!)
                tvLeader.visibility = View.VISIBLE
            } else {
                tvLeader.visibility = View.INVISIBLE
            }
            fl.removeAllViews()
            adapter = CorrectMistakesTextAdapter(0, requireContext(), textInfoList, answerArea)
            fl.setAdapter(adapter)
            for (i in textInfoList.indices) {
                val textInfo = textInfoList[i]
                val child = fl.getChildAt(i)
                drawArea(i)
                if (textInfo.stuType != -1) {
                    askList.add(textInfo)
                    var startPosition = -1
                    var endPosition = -1
                    val viewList = mutableListOf<View>()
                    if (textInfo.digitalAnswer.contains("-")) {
                        repeat(answerArea.size) {
                            startPosition =
                                textInfo.digitalAnswer.split("@|")[0].split("*|")[0].split("-")[0].toInt()
                            endPosition =
                                textInfo.digitalAnswer.split("@|")[0].split("*|")[0].split("-")[1].toInt()
                        }
                        for (j in startPosition..endPosition) {
                            viewList.add(fl.getChildAt(j))
                        }
                        drawAnswer(i, textInfo, child, viewList)
                    } else if (textInfo.stuType == 4) {
                        startPosition =
                            textInfo.digitalAnswer.split("@|")[0].split("*|")[0].toInt()
                        for (j in textInfoList.indices) {
                            if (startPosition == textInfoList[j].Index) {
                                viewList.add(fl.getChildAt(j))
                                drawAnswer(j, textInfo, child, viewList)
                            }
                        }
                    } else {
                        drawAnswer(i, textInfo, child, viewList)
                    }
                }
            }
            requireActivity().window.decorView.viewTreeObserver.addOnGlobalLayoutListener(
                mGlobalLayoutListener
            )
            fl.setOnItemClickListener { position, view ->
                if (ConstantBean.taskControlBean!!.enableAnwer == 0) {
                    return@setOnItemClickListener
                }
                inputManager.hideALlSoftInput()
                val textInfo = textInfoList[position]
                val patPunc = Pattern.compile("[\"'.,...:!?;—-]")
                val matcher = patPunc.matcher(textInfo.Text)
                if (matcher.find() && textInfo.Text.length <= 2) {
                    return@setOnItemClickListener
                }
                var enableClick = false
                if (GenreType != 3) {
                    answerArea.forEach {
                        if (textInfo.Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                                "-"
                            )[1].toInt()
                        ) {
                            enableClick = true
                        }
                    }
                    if (!enableClick) {
                        return@setOnItemClickListener
                    }
                }
                if (!(GenreID == 48 && GenreType == 2) && !(GenreID == 46 && GenreType == 2)) {
                    if (textInfo.stuType == -1 && askList.size >= answerInfoList.size) {
                        ToastTool.showWithClick(
                            requireContext(),
                            "最多有${answerInfoList.size}处错误"
                        )
                        return@setOnItemClickListener
                    }
                }
                currentOperateIndex = position
                val location = IntArray(2)
                view.getLocationInWindow(location)
                setPopupMenuData(position, textInfo.stuType)
                var isArea = false
                answerArea.forEach {
                    if (textInfo.Index == it.AnswerArea!!.split("-")[0].toInt() && answerArea.size > 1) {
                        isArea = true
                    }
                }
                val x = if (isArea) {
                    location[0] + view.width / 2 + DpUtils.dip2px(requireContext(), 8f)
                } else {
                    location[0] + view.width / 2
                }
                val y = location[1] - view.height
                textInfo.x = x
                textInfo.y = y + view.height
                view.findViewById<TextView>(R.id.tv_text)
                    .setBackgroundColor(Color.parseColor("#330099ff"))
                showPopupMenuWindow(position, x, y)
            }
        }
    }

    private val mGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
        val r = Rect()
        // 使用最外层布局填充，进行测算计算
        binding.dsv.getWindowVisibleDisplayFrame(r)
//        val svHeight = binding.dsv.height
        val heightDiff = screenHeight - r.bottom
        if (heightDiff > 100 && lastRalY != currentRalY) {
            lastRalY = currentRalY
            Handler(Looper.getMainLooper()).post {
                binding.dsv.smoothScrollTo(
                    0,
                    binding.dsv.scrollY + currentRalY - screenHeight + heightDiff + DensityUtils.dp2px(
                        context, 90f
                    )
                )
            }
        }
        if (heightDiff == 0 && lastRalY == currentRalY) {
            lastRalY = 0
        }
    }


    private fun setPopupMenuData(position: Int, type: Int) {
        popupMenuItemList.clear()
        when (type) {
            -1 -> {
                if (getAnswerType(position) == 4) {
                    popupMenuItemList.add("取消")
                } else {
                    popupMenuItemList.add("删除")
                    popupMenuItemList.add("修改")
                    popupMenuItemList.add("前增")
                    popupMenuItemList.add("后增")
                    if (GenreID == 48 && GenreType == 2 || GenreID == 47 && GenreType == 4 || GenreID == 49 && GenreType == 5) {
                        popupMenuItemList.add("无错")
                    }
                }
            }

            0, 4 -> popupMenuItemList.add("取消")
            1, 2, 3 -> {
                popupMenuItemList.add("取消")
                popupMenuItemList.add("编辑")
            }
        }
    }

    /*绘制编号*/
    @SuppressLint("SetTextI18n")
    private fun drawArea(position: Int) {
        if (ques.ChildList!![0].ItemList!!.size == 1) {
            binding.tvIndex.text = "${ques.ChildList!![0].ItemList!![0].ItemSortIndex}."
        } else if (answerArea.size > 1) {
            answerArea.forEachIndexed { index, it ->
                if (it.AnswerArea!!.split("-")[0].toInt() == textInfoList[position].Index) {
                    val child = binding.fl.getChildAt(position)
                    val tvIndex =
                        DrawView(
                            false,
                            false,
                            child,
                            ques.ChildList!![0].ItemList!![if (ques.ChildList!![0].ItemList!!.size > 1) index else 0].ItemSortIndex!!,
                            "",
                            -1,
                            1,
                            requireContext()
                        )
                    binding.flLayout.addView(tvIndex)
                }
            }
        } else if (ques.ChildList!![0].ItemList!!.size == 1) {
            binding.tvIndex.text = "${ques.ChildList!![0].ItemList!![0].ItemSortIndex}."
        }
    }

    private fun showPopupMenuWindow(position: Int, mRawX: Int, mRawY: Int) {
        val popupList = PopupList(requireContext())
        popupList.showPopupListWindow(
            binding.flLayout, position, mRawX.toFloat(), mRawY.toFloat(),
            popupMenuItemList, this
        )
    }

    override fun showPopupList(
        adapterView: View?,
        contextView: View?,
        contextPosition: Int
    ): Boolean {
        return true
    }

    override fun onPopupListClick(contextView: View?, contextPosition: Int, position: Int) {
        operateAnswer(contextPosition, position)
    }

    /**
     * @param position 操作单词下标
     * @param operateType  操作类型 0--删除 1--修改 2--前增 3--后增4--无错
     */
    private fun operateAnswer(position: Int, operateType: Int) {
        val textInfo: TextInfo = textInfoList[position]
        val childAt: View = binding.fl.getChildAt(position)
        val tvText = childAt.findViewById<TextView>(R.id.tv_text)
        var startPosition = -1
        var endPosition = -1
        answerArea.forEach {
            if (textInfo.Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                    "-"
                )[1].toInt()
            ) {
                startPosition = it.AnswerArea!!.split("-")[0].toInt()
                endPosition = it.AnswerArea!!.split("-")[1].toInt()
            }
        }
        when (popupMenuItemList[operateType]) {
            "取消" -> { //有过作答
                deleteAnswer(
                    startPosition,
                    endPosition,
                    position
                )
            }

            "删除" -> {
                if ((GenreID == 48 && GenreType == 2) || (GenreID == 46 && GenreType == 2)) {
                    if (hasAnswer(position)) {
                        val answerPosition = getAnswerPosition(position)
                        val view = tvMap[answerPosition]
                        askList.remove(textInfoList[answerPosition])
                        binding.flLayout.removeView(view)
                        tvMap.remove(answerPosition)
                        textInfoList[answerPosition].stuType = -1
                        textInfoList[answerPosition].stuAnswer = ""
//                        textInfoList[answerPosition].Index = "-1"
                        textInfoList[answerPosition].digitalAnswer = ""
                    }
                }
                setCurrentIndex(position)
                tvText.setBackgroundColor(Color.TRANSPARENT)
                textInfo.stuType = operateType
                textInfo.stuAnswer = ""
//                textInfo.Index = position.toString()
                val answerStr = CorrectMistakeUtils.toStr(position, textInfoList)
                val str = CorrectMistakeUtils.toArr(textInfo, answerStr)
                textInfo.digitalAnswer = str
                askList.add(textInfo)
                insertStuAnswer(askList)
                drawAnswer(position, textInfo, childAt, mutableListOf())
//                saveAnswerPic()
            }

            "修改", "前增", "后增", "编辑" -> {
                showEditInputPopupWindow(
                    position,
                    if (popupMenuItemList[operateType] == "编辑") textInfo.stuType else operateType,
                    textInfo.stuAnswer
                )
//                showAnimation()
                remaindStyle = true
            }

            "无错" -> {
                setCurrentIndex(position)
                val viewList = mutableListOf<View>()
                textInfoList.forEachIndexed { index, textInfo ->
                    if (textInfo.Index in startPosition..endPosition) {
                        if (textInfoList[index].stuType != -1) {
                            askList.remove(textInfoList[index])
                            textInfoList[index].stuType = -1
                            textInfoList[index].stuAnswer = ""
//                        textInfoList[i].Index = "-1"
                            textInfoList[index].digitalAnswer = ""
                            val view = tvMap[index]
                            binding.flLayout.removeView(view)
                            tvMap.remove(index)
                        }
                        viewList.add(binding.fl.getChildAt(index))
                    }
                }
                tvText.setBackgroundColor(Color.TRANSPARENT)
                textInfo.stuType = 4
                textInfo.stuAnswer =
                    if (viewList.size > 1) "$startPosition-$endPosition*|4*|@|√" else "$startPosition*|4*|@|√"
//                textInfo.Index = "$startPosition-$endPosition"
                textInfo.digitalAnswer =
                    if (viewList.size > 1) "$startPosition-$endPosition*|4*|@|√" else "$startPosition*|4*|@|√"
                askList.add(textInfo)
                insertStuAnswer(askList)
                drawAnswer(position, textInfo, childAt, viewList)
//                saveAnswerPic()
            }
        }
    }

    /*作答输入弹窗*/
    private fun showEditInputPopupWindow(
        position: Int,
        answerType: Int,
        text: String
    ) {
        val tag = when (answerType) {
            1 -> "edit"
            2 -> "front"
            else -> "back"
        }
        inputPopupWindow = NewEditInputView(tag, requireContext())
        val textInfoBean = textInfoList[position]
        val childAt = binding.fl.getChildAt(position)
        currentRalY = textInfoBean.y
        inputPopupWindow.showNow(requireActivity().supportFragmentManager, "")
        inputPopupWindow.binding.edt.requestFocus()
        inputPopupWindow.binding.edt.setText(text)
        EditTextInputUtil.disableCopyAndPaste(inputPopupWindow.binding.edt)
        SoftInputUtils.setEditTextInputSpace(inputPopupWindow.binding.edt)
        inputPopupWindow.binding.edt.setSelection(text.length)
        activity?.runOnUiThread {
            inputManager.showSoftInput(inputPopupWindow.binding.edt)
        }
//        Handler().postDelayed({ inputManager.showSoftInput() }, 10)
        inputPopupWindow.setStateListener(object : NewEditInputView.StateListener {
            override fun close() {
                childAt.findViewById<TextView>(R.id.tv_text).setBackgroundColor(Color.TRANSPARENT)
                dismissAnimation()
                Handler(Looper.getMainLooper()).post {
                    inputManager.hideALlSoftInput()
                }
                val answer = inputPopupWindow.binding.edt.text.toString().trim { it <= ' ' }
                if (answer.isNotEmpty()) {
                    if ((GenreID == 48 && GenreType == 2) || (GenreID == 46 && GenreType == 2)) {
                        if (hasAnswer(position)) {
                            val answerPosition = getAnswerPosition(position)
                            val view = tvMap[answerPosition]
                            askList.remove(textInfoList[answerPosition])
                            binding.flLayout.removeView(view)
                            tvMap.remove(answerPosition)
                            textInfoList[answerPosition].stuType = -1
                            textInfoList[answerPosition].stuAnswer = ""
//                            textInfoList[answerPosition].Index = "-1"
                            textInfoList[answerPosition].digitalAnswer = ""
                        }
                    } else {
                        if (hasAnswer(position)) {
                            val view = tvMap[position]
                            askList.remove(textInfoBean)
                            binding.flLayout.removeView(view)
                            tvMap.remove(position)
                        }
                    }
                    setCurrentIndex(position)
                    textInfoBean.stuAnswer = answer
                    textInfoBean.stuType = when (inputPopupWindow.binding.iv3rd.tag) {
                        "edit" -> 1
                        "front" -> 2
                        else -> 3
                    }
//                    textInfoBean.Index = position.toString()
                    val answerStr = CorrectMistakeUtils.toStr(position, textInfoList)
                    val str = CorrectMistakeUtils.toArr(textInfoBean, answerStr)
                    textInfoBean.digitalAnswer = str
                    askList.add(textInfoBean)
                    insertStuAnswer(askList)
                    drawAnswer(position, textInfoBean, childAt, mutableListOf())
//                saveAnswerPic()
                }
            }
        })
        inputPopupWindow.binding.edt.setOnEditorActionListener { textView: TextView?, i: Int, keyEvent: KeyEvent? ->
            if (keyEvent != null && keyEvent.keyCode == KeyEvent.KEYCODE_ENTER || i == EditorInfo.IME_ACTION_DONE) {
                inputManager.hideKeyboard()
                inputPopupWindow.dismiss()
                return@setOnEditorActionListener true
            }
            false
        }
    }

    /*绘制保存的作答*/
    @SuppressLint("UseCompatLoadingForDrawables")
    private fun drawAnswer(
        position: Int,
        textInfo: TextInfo,
        child: View,
        list: MutableList<View>
    ) {
        val stuType = textInfo.stuType
        val stuAnswer = textInfo.stuAnswer
        val isFirst = isFirstPosition(position)
        when (stuType) {
            0, 1, 2, 3 -> {
                val answerView =
                    DrawView(false, isFirst, child, stuAnswer, "", stuType, 1, requireContext())
                binding.flLayout.addView(answerView)
                tvMap.put(position, answerView)
            }

            4 -> {
                val answerView = DrawView(
                    false,
                    isFirst,
                    if (list.size > 1) "本区域无错" else "无错",
                    "",
                    list,
                    4,
                    1,
                    requireContext()
                )
                binding.flLayout.addView(answerView)
                tvMap.put(position, answerView)
            }
        }
    }

    /*删除答案*/
    private fun deleteAnswer(
        startPosition: Int,
        endPosition: Int,
        position: Int
    ) {
        if (getAnswerType(position) == 4) {
            textInfoList.forEachIndexed { index, textInfo ->
                if (textInfo.Index in startPosition..endPosition) {
                    if (textInfoList[index].stuType != -1) {
                        askList.remove(textInfoList[index])
                        textInfoList[index].stuType = -1
                        textInfoList[index].stuAnswer = ""
//                    textInfoList[i].Index = "-1"
                        textInfoList[index].digitalAnswer = ""
                        val view = tvMap[index]
                        binding.flLayout.removeView(view)
                        tvMap.remove(index)
                        insertStuAnswer(askList)
//                    saveAnswerPic()
                    }
                }
            }
        } else {
            askList.remove(textInfoList[position])
            textInfoList[position].stuType = -1
            textInfoList[position].stuAnswer = ""
//            textInfoList[position].Index = "-1"
            textInfoList[position].digitalAnswer = ""
            val view = tvMap[position]
            binding.flLayout.removeView(view)
            tvMap.remove(position)
            insertStuAnswer(askList)
//            saveAnswerPic()
        }
    }

    /*插入作答信息*/
    private fun insertStuAnswer(list: ArrayList<TextInfo>) {
        val answerList =
            ques.ChildList!![0].ItemList!!
        for (itemBeanX in answerList) {
            itemBeanX.StuAnswer = ""
        }
        for (i in list.indices) {
            val position = list[i].Index
            val answer = list[i].digitalAnswer
            if (answerArea.size > 1) {
                for (j in answerArea.indices) {
                    //根据area区间插入作答
                    if (position in answerArea[j].AnswerArea!!.split(
                            "-"
                        )[0].toInt()..answerArea[j].AnswerArea!!.split(
                            "-"
                        )[1].toInt()
                    ) {
                        answerList[if (answerList.size > 1) j else 0].StuAnswer = answer
                    }
                }
            } else {
                if (answerList.size > 1) {
                    answerList.forEachIndexed { index, item ->
                        if (answerInfoList[index].Index == position) {
                            item.StuAnswer = answer
                        }
                    }
                } else {
                    answerList[0].StuAnswer = answer
                }
            }
        }
        if (answerList.size > 1 && answerArea.size <= 1) {
            for (i in list.indices) {
                for (j in answerList.indices) {
                    if (!isCorrectPosition(list[i].Index)) {
                        if (answerList[j].StuAnswer!!.isEmpty()) {
                            answerList[j].StuAnswer = list[i].digitalAnswer
                            break
                        }
                    }
                }
            }
        }
    }

    /**
     * 判断学生作答位置是否在参考答案位置
     */
    private fun isCorrectPosition(position: Int): Boolean {
        var b = false
        answerInfoList.forEach {
            if (it.Index == position) {
                b = true
            }
        }
        return b
    }
    /*保存作答图片*/
//    private fun saveAnswerPic() {
//        if (GenreID == 48 || GenreID == 46) {
//            Handler().postDelayed({
//                paperPracticeInfo.TypeList!![index_A].QuesList[index_B].ChildList!![0].ImagePath =
//                    BitmapUtils.shotScrollView(
//                        dsv,
//                        paperId,
//                        index_A.toString(),
//                        index_B.toString()
//                    ).toString()
//            }, 10)
//        }
//    }

    /*该区域是否已作答*/
    private fun hasAnswer(position: Int): Boolean {
        var startPosition = -1
        var endPosition = -1
        var hasAnswer = false
        if ((GenreID == 48 && GenreType == 2) || (GenreID == 46 && GenreType == 2)) {
            answerArea.forEach {
                if (textInfoList[position].Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                        "-"
                    )[1].toInt()
                ) {
                    startPosition = it.AnswerArea!!.split("-")[0].toInt()
                    endPosition = it.AnswerArea!!.split("-")[1].toInt()
                }
            }
            for (i in startPosition..endPosition) {
                if (textInfoList[i].stuType != -1) {
                    hasAnswer = true
                }
            }
            return hasAnswer
        } else {
            return textInfoList[position].stuType != -1
        }
    }

    /*获取作答类型*/
    private fun getAnswerType(position: Int): Int {
        var startPosition = -1
        var endPosition = -1
        var answerType = -1
        if ((GenreID == 48 && GenreType == 2) || (GenreID == 46 && GenreType == 2)) {
            answerArea.forEach {
                if (textInfoList[position].Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                        "-"
                    )[1].toInt()
                ) {
                    startPosition = it.AnswerArea!!.split("-")[0].toInt()
                    endPosition = it.AnswerArea!!.split("-")[1].toInt()
                }
            }
            for (i in startPosition..endPosition) {
                if (textInfoList[i].stuType != -1) {
                    answerType = textInfoList[i].stuType
                }
            }
            return answerType
        } else {
            return textInfoList[position].stuType
        }
    }

    /**
     * 设置当前作答小题序号
     */
    private fun setCurrentIndex(position: Int) {
        answerArea.forEachIndexed { index, it ->
            if (textInfoList[position].Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                    "-"
                )[1].toInt()
            ) {
                currentIndex = index
            }
        }
    }

    /**
     * 获取当前作答小题序号
     */
    fun getCurrentIndex(): Int {
        if (currentIndex < 0) {
            return 0
        }
        return currentIndex
    }

    fun setCurrentItemIndex(position: Int) {
        currentIndex = position
    }

    /**
     * 根据index找出position
     */
    private fun getTextPosition(index: Int): Int {
        var position = -1
        textInfoList.forEachIndexed { i, textInfo ->
            if (textInfo.Index == index) {
                position = i
            }
        }
        return position
    }

    /*获取已作答作答下标*/
    private fun getAnswerPosition(position: Int): Int {
        var startPosition = -1
        var endPosition = -1
        var answerPosition = -1
        if ((GenreID == 48 && GenreType == 2) || (GenreID == 46 && GenreType == 2)) {
            answerArea.forEach {
                if (textInfoList[position].Index in it.AnswerArea!!.split("-")[0].toInt()..it.AnswerArea!!.split(
                        "-"
                    )[1].toInt()
                ) {
                    startPosition = it.AnswerArea!!.split("-")[0].toInt()
                    endPosition = it.AnswerArea!!.split("-")[1].toInt()
                }
            }
            for (i in startPosition..endPosition) {
                if (textInfoList[i].stuType != -1) {
                    answerPosition = i
                }
            }
            return answerPosition
        } else {
            return position
        }
    }

    /*是否是区域第一个*/
    private fun isFirstPosition(position: Int): Boolean {
        var isFirst = false
        if (answerArea.size == 10) {
            answerArea.forEach {
                if (textInfoList[position].Index == it.AnswerArea!!.split("-")[0].toInt()) {
                    isFirst = true
                }
            }
        }
        return isFirst
    }

    override fun dismissPopupList() {
        if (!remaindStyle) {
            binding.fl.getChildAt(currentOperateIndex).findViewById<TextView>(R.id.tv_text)
                .setBackgroundColor(Color.TRANSPARENT)
        }
        remaindStyle = false
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        if (isVisibleToUser) {
            inputManager.hideKeyboard() //当软禁盘存在的时候自动消除软键盘
        }
    }

    override fun onPause() {
        super.onPause()
        requireActivity().window.decorView.viewTreeObserver.removeOnGlobalLayoutListener(
            mGlobalLayoutListener
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        requireActivity().window.decorView.viewTreeObserver.removeOnGlobalLayoutListener(
            mGlobalLayoutListener
        )
    }

    fun adjustFontSize() {
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(binding.tvLeader,15)
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(binding.tvIndex,15)
    }

    fun markQuestion(){
        val showDialog = NoConfirmDialogHelper.showDialog(
            context, "改错题暂不支持标记为\"不确定\""
        ) ?: return
        Observable.timer(2000, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .`as`(RxLife.`as`(this))
            .subscribe(
                { aLong: Long? -> showDialog.dismiss() }
            ) { throwable: Throwable? -> }
    }
}